package jaux.tank.admin.config;

import java.util.Arrays;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import io.swagger.annotations.ApiOperation;
import jaux.tank.admin.config.properties.TankProperties;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * swagger配置类
 *
 * @date 2017年6月1日19:42:59
 */
@Configuration
@ConditionalOnProperty(prefix = "tank", name = "swagger-open", havingValue = "true")
public class SwaggerConfig implements WebMvcConfigurer {

	@Autowired
    public TankProperties tankProperties;

    /**
     * 增加swagger的支持
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (Boolean.TRUE.equals(tankProperties.getSwaggerOpen())) {
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        }
    }
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))                         //这里采用包含注解的方式来确定要显示的接口
//                .apis(RequestHandlerSelectors.basePackage("jaux.tank.api.controller"))    //这里采用包扫描的方式来确定要显示的接口
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(
                        Arrays.asList(
                        		new RequestParameterBuilder()
	                                .name("Response-Body-Include")
	                                .description("返回结果包含")
	                                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
//	                                .contentModel(new ModelSpecification("string"))
	                                .in(ParameterType.HEADER)
	                                .required(false)
	                                .build()
                                ,new RequestParameterBuilder()
	                                .name("Response-Body-Exclude")
	                                .description("返回结果排除")
	                                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
	                                .in(ParameterType.HEADER)
	                                .required(false)
	                                .build()
		                        ,new RequestParameterBuilder()
			                        .name("Response-Body-Exclude-Nulls")
			                        .description("是否排除null")
			                        .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
			                        .in(ParameterType.HEADER)
			                        .required(false)
			                        .build())
                        );
    }

    private ApiInfo apiInfo() {
    	String apiInfoDescription= null;
    	try {
    		apiInfoDescription = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("apiInfoDescription.html"), "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
        return new ApiInfoBuilder()
                .title("Tank Doc")
                .description(apiInfoDescription)
                .version("2.0")
                .build();
    }

}
